<template>
  <div>
    <div class="shop-cart">
      <div class="content">
        <div class="content-left" @click="toggleList">
          <div class="logo-wrapper">
            <div class="logo" :class="{ highlight: num > 0 }">
              <i
                class="iconfont icon-gouwuche"
                :class="{ highlight: 1 > 0 }"
              ></i>
            </div>
            <div class="num" v-show="num > 0">{{ num }}</div>
          </div>
          <div class="price">￥{{ totalPrice }}</div>
          <div class="desc" :class="{ highlight: num > 0 }">
            另需配送费￥{{ truefee }}元
          </div>
        </div>
        <div class="content-right" @click="toAccount">
          <div class="pay" :class="totalPrice >= 20 ? 'enough' : 'not-enough'">
            {{ pay }}
          </div>
        </div>
      </div>
      <!-- list -->
      <transition name="fold">
        <div
          class="shopcart-list"
          v-show="cardItem.length === 0 ? false : listShow"
        >
          <div class="list-header">
            <h1 class="title">购物车</h1>
            <span class="empty" @click="empty">清空购物车</span>
          </div>
          <div class="itemcontent">
            <div
              class="item-content"
              v-for="item in cardItem"
              :key="item.itemid"
            >
              <div class="content-header">
                <div class="content-thumb">
                  <div class="content-image" style="width: 100%; height: 100%">
                    <img :src="item.itemimg" alt="" />
                  </div>
                </div>
                <div class="content-content">
                  <div>
                    <div class="content-card-title">
                      {{ item.itemname }}
                    </div>
                    <div class="content-price content-ellipsis">
                      <span class="content-price-price"
                        >￥{{ item.itemprice }}</span
                      >
                      <Cardcontrol
                        :sellnum="item.sellcount"
                        :count="item.itemid"
                      />
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </transition>
    </div>
    <!-- masker -->
    <div
      class="masker"
      @click="maskerclick"
      v-show="cardItem.length === 0 ? false : listShow"
    ></div>
  </div>
</template>

<script>
import { reactive, toRefs } from "@vue/reactivity";
import Cardcontrol from "./Cardcontrol.vue";
import { computed, watch } from "@vue/runtime-core";
import { useStore } from "vuex";
import { useRouter, useRoute } from "vue-router";
import { Toast } from "vant";
import { setLocal } from "../common/utils";
export default {
  props: {
    truefee: Number,
  },
  setup() {
    const router = useRouter();
    const store = useStore();
    const route = useRoute();
    const state = reactive({
      listShow: false,
    });
    const toggleList = () => {
      if (num.value > 0) {
        state.listShow = !state.listShow;
      }
    };
    const cardItem = computed(() => {
      let newarr = store.state.itemlist.filter((v) => v.sellcount > 0);
      return newarr;
    });
    const num = computed(() => {
      return store.state.num;
    });
    const pay = computed(() => {
      if (totalPrice.value === 0) {
        return `20元起送`;
      } else if (totalPrice.value < 20) {
        return `还差${((20 - totalPrice.value).toFixed(2) * 100) / 100}起送`;
      } else {
        return `去结算`;
      }
    });
    const empty = () => {
      for (let item of store.state.itemlist) {
        item.sellcount = 0;
      }
      store.state.num = 0;
      store.state.totalPrice = 0;
      store.state.cartItem = [];
      state.listShow = false;
    };
    const toAccount = () => {
      if (totalPrice.value >= 20) {
        Toast.success("付款成功");
        router.push("/home");
        for (let item of store.state.itemlist) {
          item.sellcount = 0;
        }
        store.state.num = 0;
        store.state.totalPrice = 0;
        store.state.cartItem = [];
        state.listShow = false;
      }
    };
    const maskerclick = () => {
      state.listShow = !state.listShow;
      console.log(111);
    };
    const totalPrice = computed(() => {
      return (store.state.totalPrice.toFixed(2) * 100) / 100;
    });
    watch(
      totalPrice,
      (newVal, oldVal) => {
        console.log(newVal, oldVal);
        setLocal(`store${route.params.id}`, JSON.stringify(store.state));
      },
      { deep: true }
    );
    return {
      ...toRefs(state),
      maskerclick,
      empty,
      toggleList,
      totalPrice,
      num,
      pay,
      cardItem,
      toAccount,
    };
  },
  components: { Cardcontrol },
};
</script>

<style lang="less" scoped>
.shop-cart {
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 101;
  width: 100%;
  height: 8vh;
  background: #fdfffd;

  .content {
    display: flex;
    background: #0e0e0e;
    color: #fdfffd;
    height: 5.8vh;
    border-radius: 50px;
    width: 94%;
    margin-left: 3%;

    &-left {
      flex: 1;

      .logo-wrapper {
        vertical-align: middle;
        display: inline-block;
        position: relative;
        top: -8px;
        margin: 0 12px;
        padding: 4px;
        width: 50px;
        height: 50px;
        border-radius: 50%;
        box-sizing: border-box;
        background: #141d27;

        .logo {
          width: 100%;
          height: 100%;
          background: #8c8c8c;
          border-radius: 50%;
          text-align: center;

          &.highlight {
            background: #fdc804;
          }

          .iconfont {
            line-height: 44px;
            font-size: 28px;
            color: #80858a;

            &.highlight {
              color: #fff;
            }
          }
        }

        .num {
          position: absolute;
          top: 0;
          right: 0;
          width: 17px;
          height: 17px;
          line-height: 16px;
          text-align: center;
          border-radius: 16px;
          font-size: 12px;
          font-weight: bold;
          color: #fff;
          background: red;
        }
      }

      .price {
        display: inline-block;
        line-height: 5.8vh;
        padding-right: 12px;
        border-right: 1px solid rgba(255, 255, 255, 0.1);
        font-size: 15px;
        font-weight: 700;

        &.highlight {
          color: #fff;
        }
      }

      .desc {
        display: inline-block;
        line-height: 5.8vh;
        font-size: 12px;
        color: #808080;
        &.highlight {
          color: #fff;
        }
      }
    }

    .content-right {
      flex: 0 0 104px;
      width: 104px;
      .pay {
        height: 5.8vh;
        line-height: 5.8vh;
        text-align: center;
        font-size: 12px;
        font-weight: 700;

        &.not-enough {
          background: #0e0e0e;
          border-bottom-right-radius: 50px;
          border-top-right-radius: 50px;
          color: #808080;
        }

        &.enough {
          background: #ffc84b;
          width: 105px;
          color: #0e0e0e;
          border-bottom-right-radius: 50px;
          border-top-right-radius: 50px;
        }
      }
    }
  }

  .shopcart-list {
    position: relative;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    z-index: -1;
    transform: translate3d(0, -100%, 0);
    background: #fff;
    border-top-left-radius: 15px;
    border-top-right-radius: 15px;

    &.fold-enter-active,
    &.fold-leave-active {
      transition: all 0.5s;
    }

    &.fold-enter,
    &.fold-leave-to {
      transform: translate3d(0, 0, 0);
    }

    .list-header {
      display: flex;
      justify-content: space-between;
      padding: 0 18px;
      height: 40px;
      line-height: 40px;
      align-items: center;
      background: #fff;
      border-bottom: 1px solid #f3f5f7;
      border-top-left-radius: 15px;
      border-top-right-radius: 15px;

      .title {
        font-size: 14px;
        color: rgba(7, 17, 27, 1);
      }

      .empty {
        font-size: 12px;
        color: #909090;
      }
    }
    .itemcontent {
      overflow: scroll;
      max-height: 35vh;
      .item-content {
        position: relative;
        margin: 12px 0;
        box-sizing: border-box;
        padding: 5px 8px;
        width: 100%;
        height: 60px;
        color: #323233;
        border-bottom: 1px solid #f3f5f7;
        background: #fdfffd;
        .content-header {
          display: flex;
          .content-thumb {
            position: relative;
            flex: none;
            width: 50px;
            height: 50px;
            margin-right: 8px;
            .content-image {
              position: relative;
              display: inline-block;
              background-color: #111;
              border-radius: 2px;
              overflow: hidden;
              img {
                display: block;
                width: 100%;
                height: 100%;
              }
            }
          }
          .content-content {
            position: relative;
            display: flex;
            flex: 1;
            flex-direction: column;
            justify-content: space-between;
            div {
              display: block;
              .content-card-title {
                font-weight: 600;
                font-size: 14px;
              }
              .content-price {
                justify-content: space-between;
                display: flex;
                .content-price-price {
                  color: #fb5249;
                  font-weight: 600;
                  line-height: 34.4px;
                  overflow: hidden;
                  white-space: nowrap;
                  text-overflow: ellipsis;
                  font-size: 14px;
                }
              }
            }
          }
        }
      }
    }
  }
}

.masker {
  background: #07111b;
  opacity: 0.6;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 100;
}
</style>